﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NodaTime.TimeZones;
using NUnit.Framework;
using QuickPrompt.BusinessService;
using QuickPrompt.DataAccess;
using QuickPrompt.DomainModel;
using QuickPrompt.DomainModel.Identity;
using QuickPromt.Service;
using RedandBlue.Common.Logging;
using RedandBlue.RepositoryPattern.DataContext;
using RedandBlue.RepositoryPattern.EF6;
using RedandBlue.RepositoryPattern.UnitOfWork;

namespace QuickPrompt.Test.IntegrationTests
{
    public class PromtRepositoryTest
    {
        private IQuickPromptService _quickPromptService;
        [SetUp]
        public void Initialize()
        {
            var dataContext = new QuickPromptContext();
            _quickPromptService = new QuickPromptService(new Repository<Prompt>(dataContext, new UnitOfWork(dataContext)));
            //Utility.CreateSeededTestDatabase();
        }

        [TearDown]
        public void Cleanup()
        {
        }

        [Test]
        public void TableCheck()
        {
            Log.TableCheck();
        }

        
        [Test]
        [TestCase("US")]
        [TestCase("CA")]
        [TestCase("IN")]
        public void TimeZones(string countryCode)
        {
            IEnumerable<string> windowsZoneIds =
                TzdbDateTimeZoneSource.Default.ZoneLocations
                    .Where(x => x.CountryCode == countryCode)
                    .Select(tz => TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones
                        .FirstOrDefault(x => x.TzdbIds.Contains(tz.ZoneId)))
                    .Where(x => x != null)
                    .Select(x => x.WindowsId)
                    .Distinct();
            //var _timeZones = TimeZoneInfo.GetSystemTimeZones().ToList().Where(zone => zone.DisplayName.Contains("US"));
            //var inclusions = new[] { "Pacific", "Central", "Mountain", "Eastern" };
            //var zones = TimeZoneInfo.GetSystemTimeZones()
            //    .Where(zone => inclusions.Any(x => zone.Id.Contains(x)));
        }
        [Test(Description = "Email")]
        [TestCase(1)]
        public void EmailPrompt(int noteId)
        {
            using (IDataContextAsync context = new QuickPromptContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
            {
                //var prompt = unitOfWork.Repository<QuickNote>()
                //    .Query(x => x.Id == noteId)
                //    .Include(x => x.Prompts)
                //    .Include(x => x.User).Select().SingleOrDefault()?.Prompts.SingleOrDefault(x=>x.PromptType == PromptType.Email);

                var prompt = unitOfWork.Repository<QuickNote>()
                    .Query(x => x.Id == noteId)
                    .Include(x => x.Prompts)
                    .Include(x => x.User)
                    .Select()
                    .SelectMany(n => n.Prompts)
                    .SingleOrDefault(p => p.PromptType == PromptType.Email);

                Assert.IsNotNull(prompt);
                _quickPromptService.QuickPrompt(prompt);
            }
        }

        [TestCase(1)]
        public void TextPrompt(int noteId)
        {
            using (IDataContextAsync context = new QuickPromptContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
            {
                var prompt = unitOfWork.Repository<QuickNote>()
                    .Query(x => x.Id == noteId)
                    .Include(x => x.Prompts)
                    .Include(x => x.User)
                    .Select()
                    .SelectMany(n => n.Prompts)
                    .SingleOrDefault(p => p.PromptType == PromptType.Text);

                Assert.IsNotNull(prompt);
                _quickPromptService.QuickPrompt(prompt);
            }
        }
    }
}
